//
//  MSQRCodeController.m
//  觅尚
//
//  Created by No.8 on 2019/4/2.
//  Copyright © 2019 No.8. All rights reserved.
//

#import "MSQRCodeController.h"
#import "MSQRCodeCell.h"
#import "MSMeQRcodeController.h"
#import "MSQRCodeModel.h"
#import "MSBuidingWorkCardController.h"

@interface MSQRCodeController ()<UITableViewDelegate,UITableViewDataSource>
{
    CustomNavigationBar *naviagtionBar;
}
/** 表视图*/
@property(nonatomic,strong)UITableView *tableView;

/** 二维码数组*/
@property(nonatomic,strong)NSMutableArray *ListArray;


@end

@implementation MSQRCodeController

- (int)navBarBottom {
    if ([WRNavigationBar isIphoneX]) {
        return 88;
    } else {
        return 64;
    }
}
- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:YES];
    self.navigationController.navigationBar.hidden = YES;
    self.ListArray = [NSMutableArray new];
    Prepare *header = [Prepare headerWithRefreshingTarget:self refreshingAction:@selector(MJChiBaoZiHeaderRefresh)];
    // 隐藏时间
    header.lastUpdatedTimeLabel.hidden = YES;
    header.stateLabel.hidden = YES;
    // 马上进入刷新状态
    self.tableView.mj_header = header;
    [self.tableView.mj_header beginRefreshing];
}
- (void)MJChiBaoZiHeaderRefresh{
    [self ReloadData];
}
- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:YES];
    self.navigationController.navigationBar.hidden = YES;
    
}
- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self CreateNavigationBar];
    
    [self tableView];
    
    
    [self CreatBtoomButton];
    
    
}
- (void)CreatBtoomButton{
    /** 底部按钮*/
    UIButton * BtoomButton = [[UIButton alloc] initWithFrame:CGRectMake(0, ScreenHeight - 120 * px, ScreenWidth, 120 *px)];
    [BtoomButton setBackgroundImage:[UIImage imageNamed:@"底部按钮背景"] forState:UIControlStateNormal];
    [BtoomButton setTitle:@"绑定工牌" forState:UIControlStateNormal];
    BtoomButton.titleLabel.font = [UIFont systemFontOfSize:SYRealValue(28 / 2)];
    [BtoomButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [BtoomButton addTarget:self action:@selector(BtoomButtonClick) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:BtoomButton];
    
}
- (void)BtoomButtonClick{
    MSBuidingWorkCardController * buidingWorkVC = [[MSBuidingWorkCardController alloc] init];
    [self.navigationController pushViewController:buidingWorkVC animated:NO];
    
}
- (void)ReloadData{
    [self.ListArray removeAllObjects];
    [MBManager showLoading];
    NSDictionary * params = @{@"uid":[WYCAccountTool unarchiveuid]};
    [HTTPRequest POST:MSgetqrcode params:params succeed:^(id responseObject) {
        WYCLog(@"------------%@",responseObject);
        if (CodeStr) {
            [MBManager hideAlert];
            [self.tableView.mj_header endRefreshing];
            self.ListArray = [MSQRCodeModel mj_objectArrayWithKeyValuesArray:responseObject[@"data"]];
        }else{
            [MBManager hideAlert];
            [MBManager showBriefAlert:responseObject[@"msg"]];
            [self.tableView.mj_header endRefreshing];
        }
        [self.tableView reloadData];
    } failure:^(NSError *error) {
        [MBManager hideAlert];
        [MBManager showBriefAlert:Failed];
        [self.tableView.mj_header endRefreshing];
    }];
    
}
/** 表视图*/
- (UITableView *)tableView{
    self.extendedLayoutIncludesOpaqueBars = YES;
    if (!_tableView) {
        
        if (@available(iOS 11.0, *)) {
            
            _tableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
            
        } else {
            
            self.automaticallyAdjustsScrollViewInsets = NO;
            
        }
        _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, [self navBarBottom], ScreenWidth, ScreenHeight - [self navBarBottom] - 120 * px) style:UITableViewStylePlain];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.backgroundColor = [UIColor clearColor];
        _tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero];
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        [self.view addSubview:self.tableView];
    }
    return _tableView;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return self.ListArray.count;
    
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    return 300 * px;
    
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    
    MSQRCodeCell * cell = [MSQRCodeCell cellWithTableView:tableView];
    tableView.backgroundColor = backgroundcolor;
    cell.QRCodeModel = self.ListArray[indexPath.row];
    //无色
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    //    [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
    
    return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    MSQRCodeModel * codeModel = self.ListArray[indexPath.row];
    
    MSMeQRcodeController * MeQRcodeVC = [[MSMeQRcodeController alloc] init];
    
    MeQRcodeVC.qrcodeImg = codeModel.qrcodeImg;
    [self.navigationController pushViewController:MeQRcodeVC animated:NO];
    
}

- (void)CreateNavigationBar{
    
    self.view.backgroundColor = backgroundcolor;
    naviagtionBar = [[CustomNavigationBar alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, [self navBarBottom])];
    [self.view addSubview:naviagtionBar];
    naviagtionBar.titleLabel.text = @"二维码";
    naviagtionBar.rightBtn.hidden = YES;
    naviagtionBar.rightBtnTitle.hidden = YES;
    [self.view addSubview:naviagtionBar];
    
    [naviagtionBar.leftBtn setImage:[UIImage imageNamed:@"左箭头"] forState:UIControlStateNormal];
    [naviagtionBar.leftBtn addTarget:self action:@selector(backClick) forControlEvents:UIControlEventTouchUpInside];
    
}
- (void)backClick{
    
    [self.navigationController popViewControllerAnimated:NO];
}

@end
